﻿using Grpc.Core;
using Helloworld;
using System;
using UnityEngine;
using UnityEngine.UI;

public class GreeterClient : MonoBehaviour
{
    string svrAddr = "127.0.0.1";
    string output = "未连接";
    Channel channel;

    void Start()
    {
    }

    void OnGUI() {
        float y = 100;
        GUI.Label(new Rect(0, y, 100, 30), "服务器地址:");
        y += 30;
        svrAddr = GUI.TextField(new Rect(0, y, 100, 30), svrAddr);
        y += 30;
        if (GUI.Button(new Rect(0, y, 100, 30), "发送请求"))
        {
            Send();
        }
        y += 30;
        GUI.Label(new Rect(0, y, 400, 30), output);
    }

    void Send()
    {
        if (channel == null)
        {
            channel = new Channel(svrAddr + ":50051", ChannelCredentials.Insecure);
        }

        try
        {
            var client = new Greeter.GreeterClient(channel);
            HelloReply reply = client.SayHello(new HelloRequest { Name = "Jin Qing" });
            output = reply.Message;
        }
        catch (Exception e)
        {
            output = e.Message;
        }
    }
}
